﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.Client.WPF
{
    public class RequestManager
    {
        public static SimpleCommandsFormDataModel ExecuteChainItem(string key)
        {
            SimpleCommandsFormDataModel result = new SimpleCommandsFormDataModel();

            IBotvaChainItem chi = BotvaChineItemFactory.Get(key);
            chi.DoRequest();

            result.Url = chi.Requester.Url;
            
            result.GET = string.Join("",
                chi.Requester.GetMembers.Keys
                .Select(name => string.Format("{0} => {1}{2}", name, chi.Requester.GetMembers[name], Environment.NewLine))
                .ToArray());

            result.POST = string.Join("",
                chi.Requester.PostMembers.Keys
                .Select(name => string.Format("{0} => {1}{2}", name, chi.Requester.PostMembers[name], Environment.NewLine))
                .ToArray());

            result.Headers = 
                string.Join("", chi.Context.CommonCustomHeaders.Keys
                .Select(name => string.Format("{0} => {1}{2}", name, chi.Context.CommonCustomHeaders[name], Environment.NewLine))
                .ToArray()) 
                + Environment.NewLine + Environment.NewLine
                + string.Join("", chi.Context.CommonWellknownHeaders.Keys
                .Select(name => string.Format("{0} => {1}{2}", name, chi.Context.CommonWellknownHeaders[name], Environment.NewLine))
                .ToArray());

            result.Method = chi.Requester.Method.ToString();
                
            return result;
        }
    }
}
